﻿using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using DevExpress.XtraReports.UI;
using FlyHorse.LIMS.Model;
using FlyHorse.LIMS.WinApp.Model;
using System.Linq;
using System.Collections.Generic;
using DevExpress.Utils;

namespace FlyHorse.LIMS.WinApp.XtraReportTemplate
{
    public partial class XtraReportProduct : DevExpress.XtraReports.UI.XtraReport
    {
        public XtraReportProduct(ReportProduct report)
        {
            InitializeComponent();
            Transform(report);
        }

        private void Transform(ReportProduct report)
        {
            ReportProductPrintModel model = new ReportProductPrintModel();
            model.AddDateTime = report.AddDateTime;
            model.CustomerID = report.CustomerID;
            model.CustomerName = report.Customer.CustomerName;
            model.DateProduced = report.DateProduced;
            model.ExPlant = report.ExPlant;
            model.GoodsID = report.GoodsID;
            model.GoodsName = report.Good.GoodsName;
            model.LoginUserID = report.LoginUserID;
            model.LotNo = report.LotNo;
            model.Remark = report.Remark;
            model.ReportProductID = report.ReportProductID;
            model.ReportProductNO = report.ReportProductNO;
            model.ShippedTo = report.ShippedTo;
            model.TypeOfPack = report.TypeOfPack;
            model.Weight = report.Weight;


            List<ReportProductResultPrintModel> resultList = new List<ReportProductResultPrintModel>();
            foreach (var item in report.ReportProductResults)
            {
                ReportProductResultPrintModel result = new ReportProductResultPrintModel();
                result.Basis = item.Basis;
                result.ChineseName = item.ChineseName;
                result.Conclusion = item.Conclusion;
                result.FormOrder = item.FormOrder;
                result.MaxValue = item.MaxValue;
                result.MinValue = item.MinValue;
                result.ReportProductID = item.ReportProductID;
                result.ReportProductResultID = item.ReportProductResultID;
                result.TestItemID = item.TestItemID;
                result.TestItemNames = item.ChineseName + "\r\n(" + item.TestItem.Remark + ")";
                result.TestValue = item.TestValue;
                result.Unit = item.Unit;
                resultList.Add(result);
            }
            resultList = resultList.OrderBy(m => m.FormOrder).ToList();
            model.ReportProductResults = resultList;
            InitReport(model);
        }

        private void InitReport(ReportProductPrintModel report)
        {

            if (report.ReportProductResults.Count == 6)
            {
                Move(90F);
                SizeF sizeFxrTable1 = xrTable1.SizeF;
                sizeFxrTable1.Height += 20;
                xrTable1.SizeF = sizeFxrTable1;

                SizeF sizeFxrTable2 = xrTable2.SizeF;
                sizeFxrTable2.Height += 15;
                xrTable2.SizeF = sizeFxrTable2;
            }



            if (report.ReportProductResults.Count == 7)
            {
                Move(90F);

                SizeF sizeFxrTable1 = xrTable1.SizeF;
                sizeFxrTable1.Height += 10;
                xrTable1.SizeF = sizeFxrTable1;


                SizeF sizeFxrTable2 = xrTable2.SizeF;
                sizeFxrTable2.Height += 10;
                xrTable2.SizeF = sizeFxrTable2;
            }



            if (report.ReportProductResults.Count == 8)
            {
                Move(90F);

                SizeF sizeFxrTable1 = xrTable1.SizeF;
                sizeFxrTable1.Height += 5;
                xrTable1.SizeF = sizeFxrTable1;


                SizeF sizeFxrTable2 = xrTable2.SizeF;
                sizeFxrTable2.Height += 5;
                xrTable2.SizeF = sizeFxrTable2;
            }



            if (report.ReportProductResults.Count == 9)
            {
                Move(80F);
            }
            else if (report.ReportProductResults.Count == 10)
            {
                Move(60F);
            }
            else if (report.ReportProductResults.Count == 11)
            {
                Move(40F);
            }
            else if (report.ReportProductResults.Count == 12)
            {
                Move(20F);
            }
            else if (report.ReportProductResults.Count == 13)
            {

            }
            else if (report.ReportProductResults.Count == 14)
            {
                SizeF sizeFxrTable1 = xrTable1.SizeF;
                sizeFxrTable1.Height -= 35;
                xrTable1.SizeF = sizeFxrTable1;

                PageHeader.HeightF -= 35;


                SizeF sizeFxrTable2 = xrTable2.SizeF;
                sizeFxrTable2.Height -= 5;
                xrTable2.SizeF = sizeFxrTable2;
                Detail.HeightF -= 5;
            }

            //xrLabel4.Text = "报告编号:" + report.ReportProductNO;
            xrTableCell_GoodsName.Text = report.GoodsName;
            xrTableCell_ShippedTo.Text = report.CustomerName;
            xrTableCell_LotNo.Text = report.LotNo;
            xrTableCell_Destination.Text = report.ShippedTo;
            xrTableCell_DateProduced.Text = report.DateProduced.ToShortDateString();
            xrTableCell_TypeOfPack.Text = report.TypeOfPack;
            try
            {
                int weight = Convert.ToInt32(report.Weight);
                xrTableCell_Weight.Text = weight.ToString("N0");
            }
            catch (Exception)
            {
                xrTableCell_Weight.Text = report.Weight;
            }

            if (report.ExPlant != null)
            {
                xrTableCell_ExPlant.Text = Convert.ToDateTime(report.ExPlant).ToShortDateString();
            }

            xrLabel5.Text = "Report No." + report.ReportProductNO;

            this.DataSource = report.ReportProductResults;

            // xrTableCell7.DataBindings.Add("Text", null, "ChineseName");   //TestItemNames
            xrTableCell7.DataBindings.Add("Text", null, "TestItemNames");
            xrTableCell24.DataBindings.Add("Text", null, "Unit");
            xrTableCell33.DataBindings.Add("Text", null, "MinValue");
            xrTableCell30.DataBindings.Add("Text", null, "MaxValue");
            xrTableCell32.DataBindings.Add("Text", null, "TestValue");
            xrTableCell31.DataBindings.Add("Text", null, "Basis");

        }

        private void Move(float y)
        {
            //图片
            PointF pointPic = xrPictureBox1.LocationF;
            pointPic.Y += y;
            xrPictureBox1.LocationF = pointPic;

            PointF pointLabel1 = xrLabel1.LocationF;
            pointLabel1.Y += y;
            xrLabel1.LocationF = pointLabel1;

            PointF pointLabel2 = xrLabel2.LocationF;
            pointLabel2.Y += y;
            xrLabel2.LocationF = pointLabel2;

            PointF point = xrTable1.LocationF;
            point.Y += y;
            xrTable1.LocationF = point;
        }
    }
}
